<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Category;
use yii;


class ArticleLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getArticle($query = [], $page = 1, $limit = 10)
    {
        $article = Category::find()->filterWhere($query)->andWhere('type!=0')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $article->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $article->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'article/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getQuestion($query = [], $page = 1, $limit = 10)
    {
        $article = Category::find()->filterWhere($query)->andWhere('type=0')->orderBy('article_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $article->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $article->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'article/question',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function editArticleById($id, $params)
    {
        if(!$article = Category::findOne($id)) return false;
        $article->content = $params['content'];
        return $article->save();
    }

    public function editQuestionById($id, $params)
    {
        if(!$article = Category::findOne($id)) return false;
        $article->article_name = $params['article_name'];
        $article->content = $params['content'];
        return $article->save();
    }



    public function getById($id)
    {
        return Category::find()->where(['article_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $article = Category::findOne($id);
        if($article->status)
            $article->status = 0;
        else
            $article->status = 1;
        return $article->save();
    }

    public function addArticle($params){
        $article = new Category();
        $article->article_name = $params['article_name'];
        $article->content = $params['content'];
        return $article->save();
    }


}